package com.wky.oss;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.aliyun.oss.*;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.wky.basic.Result.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/oss")
@CrossOrigin
public class OssJavaSdkQuickStart {


    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/ossFileUpload")
    public JsonResult ossFileUpload(MultipartFile file){
        // Endpoint以华东1（杭州）为例，填写为https://oss-cn-hangzhou.aliyuncs.com，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 填写Bucket所在地域。以华东1（杭州）为例，Region填写为cn-hangzhou。
        String region = "cn-hangzhou";
        // 定义存储空间名称。
        String bucketName = "kkyq-pethome";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(
                endpoint,
                "LTAI5tSqS8JEcEUrD9kuLGye",
                "Lph27MsupUqTTbZaZi9d0xF9pmau9G");
        try {
            //1. 按照年月日创建文件夹路径
            String path = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
            //2. 原始文件名称
            String originalFilename = file.getOriginalFilename();
            //3. 获取文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //4. 文件名+后缀
            String fileName = System.currentTimeMillis()+suffix;


            // 2. 上传文件
            String objectName = path+"/"+fileName;
            ossClient.putObject(bucketName, objectName, file.getInputStream());
            log.info("文件：{}，上传成功", objectName);

            //https://kkyq-pethome.oss-cn-hangzhou.aliyuncs.com/2025/09/10/1757500926740.PNG
            //拼接访问路径
            String s = endpoint.substring(endpoint.indexOf("//") + 2);
            String url = "https://"+bucketName+"."+s+"/"+objectName;
            return JsonResult.success(url);
        } catch (Exception oe) {
            oe.printStackTrace();
            return JsonResult.error();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/deleteFile")
    public JsonResult deleteFile(String filePath){
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(
                endpoint,
                "LTAI5tSqS8JEcEUrD9kuLGye",
                "Lph27MsupUqTTbZaZi9d0xF9pmau9G");
        try {
            ossClient.deleteObject("kkyq-pethome", filePath);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }



}